## Copyright (C) 2020 deesix <deesix@tuta.io>
## Copyright (C) 2020 Sanne Wouda
## This file is part of M2-Planet.
##
## M2-Planet is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## M2-Planet is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with M2-Planet.  If not, see <http://www.gnu.org/licenses/>.

DEFINE NULL 0000000000000000

# Stack (x18 as SP, 64 bits per element)
DEFINE STR_X0,[X18,-8]! 408e1ff8
DEFINE STR_X1,[X18,-8]! 418e1ff8
DEFINE STR_X16,[X18,-8]! 508e1ff8
DEFINE STR_X17,[X18,-8]! 518e1ff8
DEFINE STR_X30,[X18,-8]! 5e8e1ff8

DEFINE LDR_X0,[X18],8 408640f8
DEFINE LDR_X1,[X18],8 418640f8
DEFINE LDR_X16,[X18],8 508640f8
DEFINE LDR_X17,[X18],8 518640f8
DEFINE LDR_X30,[X18],8 5e8640f8

DEFINE MOV_X18,SP f2030091 # mov x18, sp


# Jump/branch/call/return
DEFINE BR_X16 00021fd6
DEFINE BLR_X16 00023fd6
DEFINE RET c0035fd6

DEFINE CBZ_X0,0x14 a00000b4
DEFINE CBNZ_X0,0x14 a00000b5

DEFINE B.EQ_0x8 40000054
DEFINE B.NE_0x8 41000054
DEFINE B.LT_0x8 4b000054
DEFINE B.LE_0x8 4d000054
DEFINE B.GT_0x8 4c000054
DEFINE B.GE_0x8 4a000054

DEFINE B.CC_0x8 43000054
DEFINE B.LS_0x8 49000054
DEFINE B.CS_0x8 42000054
DEFINE B.HI_0x8 48000054

DEFINE B_0x8 02000014


# Load literals (PC-relative)
DEFINE LDRSW_X0,0x8 40000098
DEFINE LDR_W1,0x8 41000018
DEFINE LDR_W2,0x8 42000018
DEFINE LDR_W16,0x8 50000018


# Load/store/dereference
DEFINE LDR_X0,[X18] 400240f9
DEFINE STR_X0,[X1] 200000f9
DEFINE STRB_W0,[X1] 20000039
DEFINE LDR_X0,[X0] 000040f9
DEFINE LDRB_W0,[X0] 00004039


# Move data between registers
DEFINE MOV_X0,X17 e00311aa
DEFINE MOV_X1,X0 e10300aa
DEFINE MOV_X1,X18 e10312aa
DEFINE MOV_X2,X0 e20300aa
DEFINE MOV_X3,X0 e30300aa
DEFINE MOV_X4,X0 e40300aa
DEFINE MOV_X5,X0 e50300aa
DEFINE MOV_X6,X0 e60300aa
DEFINE MOV_X16,X0 f00300aa
DEFINE MOV_X16,X18 f00312aa
DEFINE MOV_X17,X16 f10310aa
DEFINE MOV_X17,X18 f10312aa


# Move constant to register
DEFINE MOV_X0,0x0 000080d2
DEFINE MOV_X0,0x1 200080d2
DEFINE MOV_X0,0x11 200280d2
DEFINE MOV_X0,0xFFFFFFFFFFFFFFFF 00008092
DEFINE MOV_W0,0xFFFFFFFF 00008012
DEFINE MOV_X1,0x0 010080d2
DEFINE MOV_X1,0x2 410080d2
DEFINE MOV_X1,0x8 010180d2
DEFINE MOV_X2,0x1 220080d2
DEFINE MOV_X0,0xFFFFFFFFFFFFFF9C 600c8092


# Arith/logic/relational
DEFINE ADD_X0,X1,X0 2000008b
DEFINE ADD_X0,X17,X0 2002008b
DEFINE ADD_X1,X18,0x8 41220091

DEFINE SUB_X0,X1,X0 200000cb
DEFINE SUB_X0,X0,X1 000001cb
DEFINE SUB_X0,X0,0x8 002000d1
DEFINE SUB_X0,X0,0x10 004000d1
DEFINE SUB_X0,X0,0x18 006000d1
DEFINE MSUB_X0,X0,X2,X1 0084029b

DEFINE MUL_X0,X1,X0 207c009b
DEFINE SDIV_X0,X1,X0 200cc09a
DEFINE SDIV_X2,X1,X0 220cc09a
DEFINE UDIV_X0,X1,X0 2008c09a
DEFINE UDIV_X2,X1,X0 2208c09a

DEFINE LSL_X0,X0,X2 0020c29a
DEFINE LSL_X0,X1,X0 2020c09a
DEFINE LSR_X0,X1,X0 2024c09a
DEFINE ASR_X0,X1,X0 2028c09a

DEFINE MVN_X0,X0 e00320aa
DEFINE AND_X0,X1,X0 2000008a
DEFINE ORR_X0,X1,X0 200000aa
DEFINE EOR_X0,X0,X1 000001ca

DEFINE CMP_X1,X0 3f0000eb


# Syscall
DEFINE MOV_X8,0xD6 c81a80d2
DEFINE MOV_X8,0x31 280680d2
DEFINE MOV_X8,0xDC 881b80d2
DEFINE MOV_X8,0x39 280780d2
DEFINE MOV_X8,0xDD a81b80d2
DEFINE MOV_X8,0x5D a80b80d2
DEFINE MOV_X8,0x30 080680d2
DEFINE MOV_X8,0x32 480680d2
DEFINE MOV_X8,0x35 a80680d2
DEFINE MOV_X8,0x11 280280d2
DEFINE MOV_X8,0x3E c80780d2
DEFINE MOV_X8,0x38 080780d2
DEFINE MOV_X8,0x3F e80780d2
DEFINE MOV_X8,0xA0 081480d2
DEFINE MOV_X8,0x104 882080d2
DEFINE MOV_X8,0x40 080880d2
DEFINE SVC_0x0 010000d4
